<template>
  <div class="publishGraduation">
    <el-breadcrumb separator="/" class="breadcrumb-bottom">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>毕业设计</el-breadcrumb-item>
      <el-breadcrumb-item>上传毕业设计安排</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="20">
      <el-col :span="5">
        <div class="">
          <el-select v-model="graduationForm.majorID" placeholder="请选择专业" class="full-line">
            <el-option v-for="item in majorData" :label="item.majorName" :value="item.majorID"></el-option>
          </el-select>
        </div>
      </el-col>
      
      <el-col :span="4">
        <div class="">
          <el-date-picker
            @change="changeGrade"
            v-model="graduationForm.grade"
            align="right"
            type="year"
            class="max-width"
            placeholder="请选择年级">
          </el-date-picker>
        </div>
      </el-col>
      
      <el-col :span="4">
        <div class="">
          <el-date-picker
            @change="changeAcademicYear"
            v-model="graduationForm.academicYear"
            align="right"
            type="year"
            class="max-width"
            placeholder="请选择学年">
          </el-date-picker>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="">
          <el-select v-model="graduationForm.term" placeholder="请选择学期" class="max-width">
            <el-option label="01" value="01"></el-option>
            <el-option label="02" value="02"></el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="5">
        <div class="overflow-clear">
          <el-button type="primary" @click="selectSearch">立即搜索</el-button>
        </div>
      </el-col>
    </el-row>
    <el-button-group class="float-right mb-5">
      <el-button type="primary" @click="toGraduationPlan">
        新增毕业设计安排
      </el-button>
    </el-button-group>
    <el-table
      :data="graduationTable"
      style="width: 100%"
    >
      <el-table-column
        prop="majorName"
        label="专业"
        min-width="120"
        sortable>
      </el-table-column>
      
      <el-table-column
        prop="grade"
        label="年级"
        min-width="80"
        sortable>
      </el-table-column>
      
      <el-table-column
        prop="academicYear"
        label="学年"
        min-width="80"
        sortable>
      </el-table-column>
      <el-table-column
        prop="term"
        label="学期"
        min-width="80">
      </el-table-column>
      <el-table-column
        label="毕业设计内容"
        min-width="150">
        <template scope="scope">
          <el-button type="text" @click.native.prevent="showGraduatePlan(scope.row.graduatePlanContent)">
            {{scope.row.graduatePlanName}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="upTime"
        label="上传时间"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="uploader"
        label="上传人"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="options"
        label="操作"
        min-width="120">
        <template scope="scope">
          <el-button
            @click="upDateInfo(scope.$index,graduationTable)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-button
            @click.native.prevent="deleteRow(scope.$index,graduationTable,scope.row.graduateProPlanID)"
            type="text"
            size="small">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="paging">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'publishGraduation',
    data() {
      return {
        graduationTable: [],
        graduationForm: {
          majorID: '',
          academicYear: '',
          term: '',
          grade:''
        },
        majorData: [],
        page: 1,
        pageSize: 10,
        pageSizes: [10, 20, 30, 40],
        total: 40,
        apiUrl: this.$until.url + '/graduatePlan/getGraduatePlan',
        initUrl: this.$until.url + '/courseDesign/getMajorDesign',
        deleteUrl: this.$until.url + '/graduatePlan/deleteGraduatePlan',
        updateUrl: this.$until.url + '/graduatePlan/updateGraduatePlan'
      }
    },
    mounted: function () {
      this.$nextTick(function () {
        this.searchRes(1, this.pageSize);
        this.gerMajorInfo();
      });
    },
    methods: {
      gerMajorInfo(){
        this.$http.get(this.initUrl)
          .then((res) => {
            this.majorData = Array.from(res.body);
          }, (err) => {
            console.log(err)
          })
      },
      upDateInfo(index, rows){
        this.$router.push({path: '/UpdateGraduationPlan?graduateProPlanID=' + rows[index].graduateProPlanID});
      },
      deleteRow(index, rows, graduateProPlanID) {

        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(this.deleteUrl, {graduateProPlanID: graduateProPlanID}, {emulateJSON: true}
          ).then((res) => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success'
            });
            rows.splice(index, 1);
          }, (err) => {
            this.$notify.error({
              title: '错误',
              message: '删除失败'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      changeAcademicYear: function (val) {
        this.graduationForm.academicYear = val;
      },
      changeGrade: function (val) {
        this.graduationForm.grade = val;
      },
      selectSearch: function () {
        this.$http.post(this.apiUrl, this.graduationForm, {
            params: {
              page: this.page,
              pageSize: this.pageSize
            },
            emulateJSON: true
          }
        ).then((res) => {
          this.graduationTable = [];
          this.total = res.body.totalNum;
          for (const element of res.body.Plan) {
            this.graduationTable.push(element);
          }
        }, (err) => {
          console.log(err);
        })
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.graduationTable = [];
        this.searchRes(val, this.pageSize);
      },
      //默认是第一页
      searchRes: function (page, pageSize) {
        this.$http.post(this.$until.url + '/graduatePlan/getGraduatePlan?page=' + page + '&pageSize=' + pageSize).then(response => {
          // total list for table

          this.total = response.body.totalNum;
          //console.log(this.total);
          for (const element of response.body.Plan) {
            this.graduationTable.push(element);
          }
        }, response => {
          alert("找不到文件searchRes");
        });
      },
      showGraduatePlan(showGraduatePlan){
        window.open(this.$until.url + '/' + showGraduatePlan)
      },
      toGraduationPlan(){
        this.$router.push({path: 'GraduationPlan'})
      }
    }

  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .max-width {
    width: 100% !important;
  }

  .add-pd-26 {
    padding-left: 1.6em;
    padding-right: 1.6em;
  }
</style>
